//设置参数
function ajaxtool(callback) {
    let xhr = new XMLHttpRequest();
    if (callback.type.toLowerCase() == 'get') {
        xhr.open("get", callback.url + "?" + callback.data);
        xhr.send()
    }
    if (callback.type.toLowerCase() == 'post') {
        xhr.open("post", callback.url);
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhr.send(callback.data);
    }
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            callback.success(xhr.responseText);
        }
    }
}


//封装获取cookie
function getCookie(key) {
    let cookie = document.cookie;
    let arr = cookie.split('; ');
    for (let i = 0; i < arr.length; i++) {
        let item = arr[i].split('=');
        if (item[0] == key) {
            return item[1];
        }
    }
    return '';
}

//设置cookie
function setCookie(key, value, day) {
    let date = new Date();
    date.setDate(date.getDate() + day);
    document.cookie = key + '=' + value + ';expires=' + date;
}

//删除cookie
function delaCookie() {
    setCookie(key, "", -1);
}


//跨域连接
function $jsonp(options, x) {
    var oScrc = document.createElement('script');
    //  window[fn]:将fn函数挂载在全局作用域下
    // 将传入的函数 转为window对象下
    var fn = 'myfn' + Math.random().toString().replace('.', '');
    // fn随机函数名
    window[fn] = options.success;
    oScrc.src = options.url + '?' + x + '=' + fn + '&' + getParams(options.data);
    document.body.appendChild(oScrc);
    oScrc.onload = function () {
        this.remove();
    }
}


// 将js对象{}转为字符串拼接的形式  username=zhangsa&password=12345
function getParams(obj) {
    var arr = [];
    for (var k in obj) {
        arr.push(k + '=' + obj[k]);
    }
    return arr.join('&');
}




//获取 
function getData(key) {
    var collection = localStorage.getItem(key);
    if (collection != null) {
        return JSON.parse(collection);
    } else {
        return [];
    }
}

//   清空
function delData() {
    localStorage.clear();
}

//   存储
function setData(key, data) {
    localStorage.setItem(key, JSON.stringify(data));
}



